package com.bishe.demo.modules.hotel.hotel.dao;

import com.bishe.demo.modules.account.entity.User;
import com.bishe.demo.modules.hotel.hotel.entity.Comment;
import com.bishe.demo.modules.hotel.hotel.entity.HotelOrder;
import com.bishe.demo.modules.hotel.hotelMain.entity.HotelMain;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface HotelOrderDao {
    @Insert("insert into hotel_order values(null,#{roomId},#{orderName},#{checkIn},#{checkOut},#{specially},#{userId},#{price},#{time}) ")
    @Options(useGeneratedKeys = true, keyProperty = "orderId", keyColumn = "order_id")
    void insertHotelOrder(HotelOrder hotelOrder);

    @Select("select count(order_id) from hotel_order where check_in<=#{checkOut} or check_out>=#{checkIn}")
    int countHotelOrder(HotelOrder hotelOrder);

    @Select("select * from hotel_order where user_id=#{userId}")
    @Results(id = "hotelOrderResult", value = {
            @Result(property = "orderId", column = "order_id"),
            @Result(column = "order_id", property = "hotelMain",
                    javaType = HotelMain.class,
                    one = @One(select = "com.bishe.demo.modules.hotel.hotelMain.dao.HotelMainDao.getHotelMainByOrderId")),
            @Result(column = "order_id", property = "comment",
                    javaType = Comment.class,
                    one = @One(select = "com.bishe.demo.modules.hotel.hotel.dao.CommentDao.getCommentByOrderId")),
            @Result(column = "user_id", property = "user",
                    javaType = User.class,
                    one = @One(select = "com.bishe.demo.modules.account.dao.UserDao.getUserByUserId")),
    })
    List<HotelOrder> getHotelOrdersByUserId(int userId);

    @Select("select * from hotel_order ho left join hotel_order_hotel_main hohm on hohm.hotel_order_id=ho.order_id where hohm.hotel_main_id=#{hotelMainId}")
    @ResultMap("hotelOrderResult")
    List<HotelOrder> getHotelOrdersByHotelMainId(int hotelMainId);
}
